package com.qianbei.common.pay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.qianbei.BuildConfig;
import com.qianbei.common.net.view.MsgAlert;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.json.JSONObject;

public class PayService {

	private PayService() {
	}

	private static PayService payService;

	public static PayService getInstance() {
		if (payService == null) {
			payService = new PayService();
		}
		return payService;
	}

	public interface PayResultListener {
		public void payResult(boolean isSuccess);
	}

	public PayResultListener payResultListener;

	@SuppressLint("HandlerLeak")
	public Handler mainThreadHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				// 支付宝支付
				String status = parseAlipay((String) msg.obj);
				if (TextUtils.equals(status, "9000")) {
					payResultListener.payResult(true);
				} else {
					payResultListener.payResult(false);
				}
			} else if (msg.what == 1) {
				// 微信支付
				int code = msg.arg1;
				
//				new MsgAlert().show("请安装微信====="+code);
				if (code == 0) {
					payResultListener.payResult(true);
				} else {
					payResultListener.payResult(false);
				}
			}
		}
	};

	// ///////////////////支付宝支付/////////////////////
	public void alipayPay(final String payObj,
			PayResultListener payResultListener, final Activity activity) {
		this.payResultListener = payResultListener;
		Runnable payRunnable = new Runnable() {

			@Override
			public void run() {
				PayTask alipay = new PayTask(activity);
				String result = alipay.pay(payObj,true);

				Message msg = new Message();
				msg.what = 0;
				msg.obj = result;
				mainThreadHandler.sendMessage(msg);
			}
		};
		Thread payThread = new Thread(payRunnable);
		payThread.start();
	}

	public String parseAlipay(String payResult) {
		String status = "0";
		if (TextUtils.isEmpty(payResult))
			return status;

		String[] resultParams = payResult.split(";");
		for (String resultParam : resultParams) {
			if (resultParam.startsWith("resultStatus")) {
				status = gatValue(resultParam, "resultStatus");
			}
		}
		return status;
	}

	private String gatValue(String content, String key) {
		String prefix = key + "={";
		return content.substring(content.indexOf(prefix) + prefix.length(),
				content.lastIndexOf("}"));
	}

	// ///////////////////微信支付/////////////////////

	public void wechatPay(JSONObject payObj,
			PayResultListener payResultListener, Context context) {
		this.payResultListener = payResultListener;
		// wechatAppID = payObj.optString("appid");

		PayReq req = new PayReq();
		req.appId = BuildConfig.WE_CHAT_APP_ID;
		req.partnerId = payObj.optString("partnerid");
		req.prepayId = payObj.optString("prepayid");
		req.packageValue = payObj.optString("package");
		req.nonceStr = payObj.optString("noncestr");
		req.timeStamp = payObj.optString("timestamp");
		req.sign = payObj.optString("sign");

		IWXAPI wxApi = WXAPIFactory.createWXAPI(context, null);
		wxApi.registerApp(BuildConfig.WE_CHAT_APP_ID);

		if (isWXAppInstalledAndSupported(wxApi)) {
			wxApi.sendReq(req);
		} else {
			new MsgAlert().show("请安装微信");
			this.payResultListener.payResult(false);
		}

	}

	/* 判断用户是否安装 微信* */

	private boolean isWXAppInstalledAndSupported(IWXAPI wxApi) {

		boolean sIsWXAppInstalledAndSupported = wxApi.isWXAppInstalled()
				&& wxApi.isWXAppSupportAPI();

		return sIsWXAppInstalledAndSupported;
	}

}
